﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.Controls.HeaderGridView;
using YJKExcelFormTool.Creator;
using YJKExcelFormTool.DataDefine;
using YJKExcelFormTool.Kits;
using YJKExcelFormTool.Utilities;

namespace YJKExcelFormTool.Forms
{
	public partial class TableCommonForm : System.Windows.Forms.Form
	{
		public TableInfos TheTableInfos
		{
			get
			{
				return this.m_TableInfos;
			}
		}

		public TableCommonForm(ExternalCommandData cmdData, string tableSettingFile, string customTableInitFile, string familySymbolTypePath, string professionType, List<string> propertesKey, Dictionary<string, string> dict)
		{
			this.m_Revit = cmdData;
			this.m_TableSettingFile = tableSettingFile;
			this.m_CustomTableInitFile = customTableInitFile;
			this.m_FamilySymbolTypePath = familySymbolTypePath;
			this.m_ProfessionType = professionType;
			this.m_PropertesKey = propertesKey;
			this.m_Dict = dict;
			this.InitializeComponent();
			this.InitData();
			this.m_Previewer = new Previewer(this, this.grpBoxHeader, 5, 15, 540, 244, 1u);
			base.MouseWheel += this.m_Previewer.MouseWheel;
		}

		protected override void DefWndProc(ref Message m)
		{
			int msg = m.Msg;
			if (msg == 1125)
			{
				if (m.WParam.ToInt32() == 1)
				{
					if (this.m_TableInfos.IsNull())
					{
						return;
					}
					TableDrawingKitForPicture tableDrawingKitForPicture = new TableDrawingKitForPicture(this.m_TableInfos.Head, this.m_TableInfos.FontHeight());
					try
					{
						tableDrawingKitForPicture.Drawing();
					}
					catch (Exception)
					{
					}
					this.m_Previewer.Curves = tableDrawingKitForPicture.Curves;
					this.m_Previewer.TextNotes = tableDrawingKitForPicture.Texts;
					this.m_Previewer.FontName = this.m_TableInfos.FontName();
					this.m_Previewer.FontHeight = Common.MMToFeet(this.m_TableInfos.FontHeight());
					return;
				}
			}
			else
			{
				base.DefWndProc(ref m);
			}
		}

		private void ckbSelectHeader_MouseClick(object sender, MouseEventArgs e)
		{
			bool @checked = this.ckbExcelHead.Checked;
			List<SymbolTypeHeadInfos> headSymbolTypeList = TableCreator.GetHeadSymbolTypeList(this.m_Revit, this.m_FamilySymbolTypePath);
			if (headSymbolTypeList.Count == 0)
			{
				if (@checked)
				{
					this.ckbSelectHeader.Checked = false;
					this.ckbExcelHead.Checked = true;
				}
				else
				{
					this.ckbExcelHead.Checked = false;
					this.ckbSelectHeader.Checked = true;
				}
				YJKMessageBox.Show("当前族库中缺少" + this.m_ProfessionType + "表头类型，请添加表头类型后，重新选择");
				return;
			}
			List<string> propertesKey = this.m_PropertesKey;
			using (SelectTableHeaderForm selectTableHeaderForm = new SelectTableHeaderForm(this.m_Revit, headSymbolTypeList, propertesKey, this.m_TableInfos.FontName(), this.m_TableInfos.FontHeight(), this.m_TableInfos.FontWidthScale()))
			{
				if (selectTableHeaderForm.ShowDialog(this) != DialogResult.OK)
				{
					if (@checked)
					{
						this.ckbSelectHeader.Checked = false;
						this.ckbExcelHead.Checked = true;
						return;
					}
					this.ckbExcelHead.Checked = false;
					this.ckbSelectHeader.Checked = true;
					return;
				}
				else
				{
					new GraphPaperDirectoryCreator(this.m_Revit);
					this.m_TableInfos.Head = selectTableHeaderForm.SelectTableHead;
					this.ckbSelectHeader.Checked = true;
					this.ckbExcelHead.Checked = false;
					this.ChangeCellSizeSetting(false);
				}
			}
			this.Refresh();
		}

		private void ckbExcelHead_MouseClick(object sender, MouseEventArgs e)
		{
			bool @checked = this.ckbSelectHeader.Checked;
			OpenFileDialog openFileDialog = new OpenFileDialog();
			openFileDialog.InitialDirectory = "c:\\";
			openFileDialog.Filter = "文本文件|*.*|Excel文件|*.xlsx|所有文件|*.*";
			openFileDialog.RestoreDirectory = true;
			openFileDialog.FilterIndex = 1;
			if (openFileDialog.ShowDialog(this) == DialogResult.OK)
			{
				string fileName = openFileDialog.FileName;
				List<string> propertesKey = this.m_PropertesKey;
				using (ImportExcelTableHeadForm importExcelTableHeadForm = new ImportExcelTableHeadForm(fileName, propertesKey, this.m_TableInfos.FontName(), this.m_TableInfos.FontHeight(), this.m_TableInfos.FontWidthScale()))
				{
					if (importExcelTableHeadForm.ShowDialog(this) != DialogResult.OK)
					{
						if (@checked)
						{
							this.ckbExcelHead.Checked = false;
							this.ckbSelectHeader.Checked = true;
							return;
						}
						this.ckbSelectHeader.Checked = false;
						this.ckbExcelHead.Checked = true;
						return;
					}
					else
					{
						new GraphPaperDirectoryCreator(this.m_Revit);
						this.m_TableInfos.Head = importExcelTableHeadForm.SelectTableHead;
						this.ckbSelectHeader.Checked = false;
						this.ckbExcelHead.Checked = true;
						this.ChangeCellSizeSetting(false);
					}
				}
				this.Refresh();
				return;
			}
			if (@checked)
			{
				this.ckbExcelHead.Checked = false;
				this.ckbSelectHeader.Checked = true;
				return;
			}
			this.ckbSelectHeader.Checked = false;
			this.ckbExcelHead.Checked = true;
		}

		private void cmbTextNoteType_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.m_TableInfos.SetFontSetting(this.GetCurrentTextNoteType());
			this.Refresh();
		}

		private void rdbAutoCellsSize_CheckedChanged(object sender, EventArgs e)
		{
			this.SetAutoCellsSize(((RadioButton)sender).Checked);
			this.Refresh();
		}

		private void DGVColsWidth_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			System.Windows.Forms.TextBox textBox = e.Control as System.Windows.Forms.TextBox;
			if (textBox != null)
			{
				textBox.TextChanged += this.DGVTextBoxTextChanged;
			}
		}

		private void InitData()
		{
			GraphPaperDirectoryCreator graphPaperDirectoryCreator = new GraphPaperDirectoryCreator(this.m_Revit);
			this.m_TableInfos = graphPaperDirectoryCreator.ReadTableSetting(this.m_TableSettingFile);
			if (this.m_TableInfos.Head.HeadType == TableHead._Tag_HeadType.HT_FAMILY)
			{
				this.ckbSelectHeader.Checked = true;
				this.ckbExcelHead.Checked = false;
			}
			else
			{
				this.ckbSelectHeader.Checked = false;
				this.ckbExcelHead.Checked = true;
			}
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_Revit.Application.ActiveUIDocument.Document);
			filteredElementCollector.OfClass(typeof(TextNoteType));
			int num = 0;
			int selectedIndex = 0;
			foreach (Element element in filteredElementCollector)
			{
				TextNoteType textNoteType = (TextNoteType)element;
				this.cmbTextNoteType.Items.Add(textNoteType.Name);
				if (textNoteType.Name.CompareTo(this.m_TableInfos.FontName()) == 0)
				{
					selectedIndex = num;
				}
				num++;
			}
			if (this.cmbTextNoteType.Items.Count > 0)
			{
				this.cmbTextNoteType.SelectedIndex = selectedIndex;
			}
		}

		private TextNoteType GetCurrentTextNoteType()
		{
			TextNoteType result = null;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_Revit.Application.ActiveUIDocument.Document);
			filteredElementCollector.OfClass(typeof(TextNoteType));
			foreach (Element element in filteredElementCollector)
			{
				TextNoteType textNoteType = (TextNoteType)element;
				if (textNoteType.Name.CompareTo(this.cmbTextNoteType.Text) == 0)
				{
					result = textNoteType;
					break;
				}
			}
			return result;
		}

        private YArchitech.Controls.HeaderGridView.DataGridViewEx InitDataGridView()
		{
			return new YArchitech.Controls.HeaderGridView.DataGridViewEx(new Container())
			{
				AllowUserToAddRows = false,
				AllowUserToOrderColumns = true,
				AutoFit = true,
				AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill,
				ColumnDeep = 1,
				ColumnHeadersHeight = 22,
				ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing,
				Dock = DockStyle.Fill,
				Location = new System.Drawing.Point(0, 0),
				Name = "gridView",
				ReadOnly = true,
				RowHeadersWidth = 30,
				RowTemplate = 
				{
					Height = 23
				},
				SelectionMode = DataGridViewSelectionMode.FullRowSelect,
				ShowRowNumber = false,
				Size = new Size(745, 216),
				TabIndex = 0,
				Title = null
			};
		}

		private void ReplaceTableColumnWidth(List<string> oldLeafColumnNames, List<double> oldLeafColumnWidth)
		{
			List<string> list = this.m_TableInfos.EffectiveColumnNameList();
			List<double> list2 = this.m_TableInfos.EffectiveColumnWidthList();
			if (list.Count != list2.Count)
			{
				return;
			}
			for (int i = 0; i < oldLeafColumnNames.Count; i++)
			{
				string oldColumnName = oldLeafColumnNames[i];
				int num = list.FindIndex((string name) => name.CompareTo(oldColumnName) == 0);
				if (num >= 0)
				{
					list2[num] = oldLeafColumnWidth[i];
				}
			}
			this.m_TableInfos.SetEffectiveColumnWidthList(list2);
		}

		private void ChangeCellSizeSetting(bool isClearData)
		{
			if (isClearData)
			{
				this.DGVColsWidth.Rows.Clear();
				return;
			}
			if (this.rdbAutoCellsSize.Checked)
			{
				return;
			}
			List<string> list = this.m_TableInfos.EffectiveColumnNameList();
			List<double> list2 = this.m_TableInfos.EffectiveColumnWidthList();
			this.DGVColsWidth.Rows.Clear();
			if (list.Count > 0)
			{
				this.DGVColsWidth.RowCount = list.Count;
				for (int i = 0; i < list.Count; i++)
				{
					this.DGVColsWidth.Rows[i].Cells[0].Value = list[i];
					int num = (int)(AssistFunc.feetToMM(list2[i]) * 10.0 + 0.5) / 10;
					this.DGVColsWidth.Rows[i].Cells[1].Value = num.ToString();
				}
			}
		}

		private void SetAutoCellsSize(bool autoCellsSize)
		{
			if (autoCellsSize)
			{
				this.m_TableInfos.AutoCellsSize = true;
				this.ChangeCellSizeSetting(true);
				this.DGVColsWidth.Enabled = false;
				return;
			}
			this.m_TableInfos.AutoCellsSize = false;
			this.DGVColsWidth.Enabled = true;
			this.ChangeCellSizeSetting(false);
		}

		private void DGVTextBoxTextChanged(object sender, EventArgs e)
		{
			int num = 1;
			if (this.DGVColsWidth.CurrentCell.ColumnIndex == num)
			{
				int rowIndex = this.DGVColsWidth.CurrentCell.RowIndex;
				string text = ((System.Windows.Forms.TextBox)sender).Text;
				int num2 = 0;
				try
				{
					num2 = Convert.ToInt32(text);
				}
				catch (Exception)
				{
					double effectiveRowItemWidthByIndex = this.m_TableInfos.Head.GetEffectiveRowItemWidthByIndex(rowIndex);
					((System.Windows.Forms.TextBox)sender).Text = AssistFunc.feetToMM(effectiveRowItemWidthByIndex).ToString();
					return;
				}
				if (num2 <= 0 || num2 > 1000)
				{
					double effectiveRowItemWidthByIndex2 = this.m_TableInfos.Head.GetEffectiveRowItemWidthByIndex(rowIndex);
					((System.Windows.Forms.TextBox)sender).Text = AssistFunc.feetToMM(effectiveRowItemWidthByIndex2).ToString();
					YJKMessageBox.Show("宽度不能小于等于零，或大于1000");
					return;
				}
				this.m_TableInfos.Head.SetEffectiveRowItemWidthByIndex(rowIndex, AssistFunc.mmToFeet((double)num2));
				this.Refresh();
			}
		}

		public bool SaveData(ref string message)
		{
			GraphPaperDirectoryCreator graphPaperDirectoryCreator = new GraphPaperDirectoryCreator(this.m_Revit);
			string text = this.cmbTextNoteType.Text;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_Revit.Application.ActiveUIDocument.Document);
			filteredElementCollector.OfClass(typeof(TextNoteType));
			foreach (Element element in filteredElementCollector)
			{
				TextNoteType textNoteType = (TextNoteType)element;
				if (text.CompareTo(textNoteType.Name) == 0)
				{
					this.m_TableInfos.SetFontSetting(textNoteType);
					break;
				}
			}
			if (this.rdbAutoCellsSize.Checked)
			{
				this.m_TableInfos.AutoCellsSize = true;
			}
			else
			{
				this.m_TableInfos.AutoCellsSize = false;
				List<double> list = new List<double>();
				for (int i = 0; i < this.m_TableInfos.Head.GetEffectiveRow().Count; i++)
				{
					double num;
					try
					{
						num = AssistFunc.mmToFeet(double.Parse((string)this.DGVColsWidth.Rows[i].Cells[1].Value));
					}
					catch (FormatException)
					{
						message = "列（" + (string)this.DGVColsWidth.Rows[i].Cells[0].Value + ")宽度数据填写错误。";
						return false;
					}
					list.Add(num);
					this.m_TableInfos.Head.SetEffectiveRowItemWidthByIndex(i, num);
				}
			}
			graphPaperDirectoryCreator.WriteTableSetting(this.m_TableInfos, this.m_TableSettingFile);
			return true;
		}

		private ExternalCommandData m_Revit;

		private string m_TableSettingFile = "";

		private string m_CustomTableInitFile = "";

		private string m_FamilySymbolTypePath = "";

		private string m_ProfessionType = "";

		private List<string> m_PropertesKey = new List<string>();

		private Dictionary<string, string> m_Dict = new Dictionary<string, string>();

		private TableInfos m_TableInfos;

		private Previewer m_Previewer;
	}
}
